﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entity;
using System.Text;
using AutoGear.BLL;
using ECommerce.Library.DataStructure;
using ECommerce.Library;
using System.Web.UI.HtmlControls;
using System.IO;
using eShop.Model;
using eShop.DataAccess;
using eShop.Business;

public partial class partners_products : SitePublicBasePage
{
    public String PostId
    {
        get
        {
            try
            {
                return Page.RouteData.Values["partnerId"].ToString();
            }
            catch
            {
                return string.Empty;
            }
        }
    }

    protected StringBuilder MyData = new StringBuilder();
    private int _repeatedColumns = 3;
    private int _maxItem = 9;

    public int RepeatedColumns
    {
        get { return _repeatedColumns; }
        set { _repeatedColumns = value; }
    }

    public int MaxItem
    {
        get { return _maxItem; }
        set { _maxItem = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            HttpContext.Current.Session["_PartnerName"] = null;
            HttpContext.Current.Session["_BrandImageUrl"] = null;
            HttpContext.Current.Session["_PartnerID"] = null;
            LoadPartnerInfo();
            
           
        }
    }

    private void LoadPartnerInfo()
    {
        if (!string.IsNullOrEmpty(PostId))
        {
            Manufacturer brand = ManufacturerManager.GetByBrandUrl(PostId);
            if (brand != null)
            {
                _PartnerID = brand.ProductBrandId;
                //litPartnerName.Text = "Welcome " + brand.BrandName + " members";
                string bannerUrl = "~/upload/partner-banners/" + brand.BrandBannerImageUrl;
                FileInfo fInfo = new FileInfo(Server.MapPath(bannerUrl));
                if (!fInfo.Exists)
                {
                    bannerUrl = "~/upload/banner-polar.png"; 
                }
                imgBanner.ImageUrl = bannerUrl;
                LoadRepeaterItems();
            }
            else
            {
                //litPartnerName.Text = "No partner exist";
            }
        }
        else
        {
            //litPartnerName.Text = "No partner exist";
        }
    }

  

    protected void LoadRepeaterItems()
    {
        ReadWriteCustomCollection<Product> featuredProduct = Product.GetAllFeaturedProductByPartnerName(PostId);
        if (featuredProduct != null && featuredProduct.Count > 0)
        {
            rptHorizontal.DataSource = featuredProduct;
            rptHorizontal.DataBind();
            uccResponseMessage.Visible = false;
        }
        else
        {
            uccResponseMessage.SetFailMessage("No featured product listed for this partner");
            uccResponseMessage.Visible = true;
        }
    }

   

    protected void Horizontal_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        try
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Product product = (Product)e.Item.DataItem;
                string[] words = product.ProductUrl.Split('/');
                string lasturl = string.Empty;
                foreach (string word in words)
                {
                    if (word.Contains(".aspx"))
                    {
                        lasturl = word.ToString();
                    }
                }
                // product image
                Image imgProduct = (Image)e.Item.FindControl("imgProduct");
                System.Drawing.Size originalImageDimension = ImageResizer.GetImageDimension(product.ProductImageSmallThumbnail);
                if (originalImageDimension.Width > 150)
                {
                    imgProduct.Style.Add("width", "150px;");
                }

                imgProduct.ImageUrl = ImageResizer.CheckImageMissing(product.ProductImageSmallThumbnail, AutoGear.BLL.SiteSettings.NoImageSmallThumbnailUrl);
                string imgUrl = imgProduct.ImageUrl.Replace(SiteSettings.SiteBaseUrl, "~");
                System.Drawing.Size imageSize = ImageResizer.GetImageDimension(imgUrl);
                if (imageSize.Height > 120)
                {
                    Unit imageHeight = new Unit(120);
                    imgProduct.Height = imageHeight;
                }
                HyperLink hlkProductPictureLink = (HyperLink)e.Item.FindControl("hlkProductPictureLink");
                //hlkProductPictureLink.NavigateUrl = product.Category.CategoryUrl + lasturl;
                hlkProductPictureLink.NavigateUrl = "~/partner/" + PostId + product.ProductUrl.Replace("~", "");

                HyperLink hlkProductNameLink = (HyperLink)e.Item.FindControl("hlkProductNameLink");
                hlkProductNameLink.Text = product.ProductName;
                
                //hlkProductNameLink.NavigateUrl = product.Category.CategoryUrl + lasturl;
                //hlkProductNameLink.NavigateUrl = "~/partner/" + PostId + "/partners-product-details.aspx?prodId=" + product.ProductId;
                hlkProductNameLink.NavigateUrl = "~/partner/" + PostId + product.ProductUrl.Replace("~","");

                Label lblProductName2 = (Label)e.Item.FindControl("lblProductName2");
                lblProductName2.Text = product.ProductName2;
                // product price
               
                Label lblProductPrice = (Label)e.Item.FindControl("lblProductPrice");
                lblProductPrice.Text = ECommerce.Library.ECommerce.Utility.FormatPriceText(product.ProductPrice);
                //if (product.StandardCost > 0)
                //{
                //    Label lblSalePrice = (Label)e.Item.FindControl("lblSalePrice");
                //    lblProductPrice.Attributes.CssStyle.Add("text-decoration", "line-through !important;");
                //    lblSalePrice.Text = ECommerce.Library.ECommerce.Utility.FormatPriceText(product.StandardCost);
                //    lblquickPrice.Text = ECommerce.Library.ECommerce.Utility.FormatPriceText(product.StandardCost);
                //}
                //else
                //{
                //    lblProductPrice.CssClass = "product-list-item-price2";
                //    lblquickPrice.Text = ECommerce.Library.ECommerce.Utility.FormatPriceText(product.ProductPrice);
                //}


            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    protected void rptTooltipContent_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Product p = (Product)e.Item.DataItem;

            if (p != null)
            {
                HtmlImage imgProductImage = (HtmlImage)e.Item.FindControl("imgProductImage");
                imgProductImage.Src = p.ProductImageDetailSize;
            }
        }
    }
}